<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Cellphonezone
 * @package     Cellphonezone_Repay
 * @copyright   Copyright (c) 2011 Cellphonezone 
 * @author      $Id$
 */
class Cellphonezone_Repay_Model_Paypal_Standard extends Mage_Paypal_Model_Standard
{
     /**
     * Return form field array
     *
     * @return array
     */
    public function getStandardCheckoutFormFields($orderId)
    {    	
        $order = Mage::getModel('sales/order')->loadByAttribute('entity_id',$orderId);
		$orderIncrementId = $order->getIncrementId();
        if(!$this->_isAllowCurrency($order))
        {
        	return parent::getStandardCheckoutFormFields();
        }
        $api = Mage::getModel('paypal/api_standard')->setConfigObject($this->getConfig());
        $api->setOrderId($orderIncrementId)
            ->setCurrencyCode($order->getOrderCurrencyCode())
            //->setPaymentAction()
            ->setOrder($order)
            ->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
            ->setReturnUrl(Mage::getUrl('paypal/standard/success'))
            ->setCancelUrl(Mage::getUrl('sales/order/history'));

        // export address
        $isOrderVirtual = $order->getIsVirtual();
        $address = $isOrderVirtual ? $order->getBillingAddress() : $order->getShippingAddress();
        if ($isOrderVirtual) {
            $api->setNoShipping(true);
        } elseif ($address->validate()) {
            $api->setAddress($address);
        }

        // add cart totals and line items
        $api->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
            ->setIsLineItemsEnabled($this->_config->lineItemsEnabled)
        ;
        $api->setCartSummary($this->_getAggregatedCartSummary());
        
        $result = $api->getStandardCheckoutRequest();
        return $result;
    } 
    
    
    protected function _isAllowCurrency($order)
    {
    	$currenciesArr = explode(',',Mage::getStoreConfig('paypal/account/allow_currencies',$order->getStore()));
    	if(in_array($order->getOrderCurrencyCode(), $currenciesArr))
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    } 
	/**
     * Aggregated cart summary label getter
     *
     * @return string
     */
    private function _getAggregatedCartSummary()
    {
        if ($this->_config->lineItemsSummary) {
            return $this->_config->lineItemsSummary;
        }
        return Mage::app()->getStore($this->getStore())->getFrontendName();
    }	
}
